{extend name="public/base" /}

{block name="body"}
    <div class="layui-card">
        <div class="layui-card-body">
            <table id="tag-table" lay-filter="tag-table" ></table>
        </div>
    </div>
    <script type="text/html" id="tag-toolbar">
        <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
            <i class="layui-icon layui-icon-add-1"></i>
            新增
        </button>
        <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
            <i class="layui-icon layui-icon-delete"></i>
            删除
        </button>
    </script>
    <script type="text/html" id="tag-table-bar">
        <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i></button>
        <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i class="layui-icon layui-icon-delete"></i></button>
    </script>
  
    {__block__}

    <script>
        layui.use(['toast', 'common'], function() {
            var $ = layui.jquery
            ,layer = layui.layer
            ,table = layui.table
            ,form = layui.form;

            let toast = layui.toast;
            let common = layui.common;

            //第一个实例
            table.render({
                elem: '#tag-table'
                ,url: "{:url('content.tag/list')}" //数据接口
                ,page: true //开启分页
                ,cols: [[ //表头
                    {type: 'numbers', fixed: 'left'}
                    ,{field: 'name', title: '名称', width:100}
                    ,{field: 'ename', title: '别名', width:100}
                    ,{field: 'keywords', title: '关键词', width:150}
                    ,{field: 'description', title: '摘要', minWidth:300}
                    ,{field: 'title', title: 'seo标题', width:200}
                    ,{field: 'time', title: '时间', width:120}
                    ,{title:'操作', toolbar: '#tag-table-bar', width:100}
                ]],
                skin: 'line',
                toolbar: '#tag-toolbar',
                limit: 100,
                text: '对不起，加载出现异常！'
            });

            table.on('tool(tag-table)', function(obj) {
                if (obj.event === 'remove') {
                    window.remove(obj);
                } else if (obj.event === 'edit') {
                    window.edit(obj);
                }
            });

            table.on('toolbar(tag-table)', function(obj) {
                if (obj.event === 'add') {
                    window.add();
                } else if (obj.event === 'refresh') {
                    window.refresh();
                } else if (obj.event === 'batchRemove') {
                    window.batchRemove(obj);
                }
            });

            form.on('submit(Tag-link-search)', function(data){
                $.post("{:url('Tag/list')}", {"tag":data.value}, function (data){
                        if (data.code == -1){
                            layer.open({content:data.msg,icon:5,anim:6});
                        }
                    }
                );
                //执行重载
            table.reload('tag-link', {
                where: {tag: data.value}
                ,page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
            });

            window.add = function() {
                layer.open({
                    type: 2,
                    title: '新增',
                    shade: 0.1,
                    area: [common.isModile()?'100%':'450px', common.isModile()?'100%':'500px'],
                    content: 'add.html'
                });
            }

            window.edit = function(obj) {
                layer.open({
                    type: 2,
                    title: '修改',
                    shade: 0.1,
                    area: [common.isModile()?'100%':'450px', common.isModile()?'100%':'500px'],
                    content: 'edit.html?id=' + obj.data.id
                });
            }

            window.remove = function(obj) {
                layer.confirm('确定要删除该用户', {
                    icon: 3,
                    title: '提示'
                }, function(index) {
                    layer.close(index);
                    let loading = layer.load();
                    $.ajax({
                        url: "{:url('content.tag/delete')}?id=" + obj.data['id'],
                        dataType: 'json',
                        type: 'delete',
                        success: function(result) {
                            layer.close(loading);
                            if (result.code === 0) {
                                layer.msg(result.msg, {
                                    icon: 1,
                                    time: 1000
                                }, function() {
                                    obj.del();
                                });
                            } else {
                                layer.msg(result.msg, {
                                    icon: 2,
                                    time: 1000
                                });
                            }
                        }
                    })
                });
            }

            window.batchRemove = function(obj) {
                var checkIds = common.checkField(obj,'id');
                if (checkIds === "") {
                    layer.msg("未选中数据", {
                        icon: 3,
                        time: 1000
                    });
                    return false;
                }

                layer.confirm('确定要删除这些用户', {
                    icon: 3,
                    title: '提示'
                }, function(index) {
                    layer.close(index);
                    let loading = layer.load();
                    $.ajax({
                        url: "{:url('content.tag/delete')}",
                        dataType: 'json',
                        type: 'delete',
                        data:{"id":checkIds},
                        success: function(result) {
                            layer.close(loading);
                            if (result.success) {
                                layer.msg(result.msg, {
                                    icon: 1,
                                    time: 1000
                                }, function() {
                                    table.reload('tag-table');
                                });
                            } else {
                                layer.msg(result.msg, {
                                    icon: 2,
                                    time: 1000
                                });
                            }
                        }
                    })
                });
            }

            window.refresh = function(param) {
                table.reload('tag-table');
            }

        });
    </script>
{/block}